home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / zvmem.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  6KB  |  190 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zvmem.c */
  20. /* "Virtual memory" operators for Ghostscript */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "alloc.h"
  25. #include "estack.h"            /* for checking in restore */
  26. #include "dict.h"            /* ditto */
  27. #include "dstack.h"
  28. #include "save.h"
  29. #include "state.h"
  30. #include "store.h"
  31. #include "gsmatrix.h"            /* for gsstate.h */
  32. #include "gsstate.h"
  33.  
  34. /* Imported operators */
  35. extern int zgsave(P1(os_ptr));
  36. extern int zgrestore(P1(os_ptr));
  37.  
  38. /* Import the routine for setting the attributes of the identity matrix. */
  39. extern void init_identity_matrix(P0());
  40.  
  41. /* 'Save' structure */
  42. typedef struct vm_save_s vm_save;
  43. struct vm_save_s {
  44.     alloc_save *asave;        /* allocator save */
  45.     gs_state *gsave;        /* old graphics state */
  46. };
  47.  
  48. /* - save <save> */
  49. int
  50. zsave(register os_ptr op)
  51. {    vm_save *vmsave = (vm_save *)alloc(1, sizeof(vm_save), "zsave");
  52.     alloc_save *asave;
  53.     int code;
  54.     gs_state *prev;
  55.     if ( vmsave == 0 ) return_error(e_VMerror);
  56.     asave = alloc_save_state();
  57.     if ( asave == 0 )
  58.        {    alloc_free((char *)vmsave, 1, sizeof(vm_save), "zsave");
  59.         return_error(e_VMerror);
  60.        }
  61.     vmsave->asave = asave;
  62.     code = zgsave(op);
  63.     if ( code < 0 ) return code;
  64.     /* Cut the chain so we can't grestore past here. */
  65.     prev = gs_state_swap_saved(igs, (gs_state *)0);
  66.     code = zgsave(op);
  67.     if ( code < 0 ) return code;
  68.     vmsave->gsave = prev;
  69.     push(1);
  70.     make_tv(op, t_save, psave, vmsave);
  71.     init_identity_matrix();        /* update l_new attribute */
  72.     return zgsave(op);
  73. }
  74.  
  75. /* <save> restore - */
  76. private int restore_check_stack(P3(const ref *, const ref *, const alloc_save *));
  77. private void restore_fix_stack(P3(ref *, ref *, const alloc_save *));
  78. int
  79. zrestore(register os_ptr op)
  80. {    vm_save *vmsave;
  81.     alloc_save *asave;
  82.     gs_state *prev;
  83.     check_type(*op, t_save);
  84.     vmsave = op->value.psave;
  85.     if ( vmsave == 0 )        /* invalidated save */
  86.         return_error(e_invalidrestore);
  87.     asave = vmsave->asave;
  88.     /* Check the contents of the stacks. */
  89.        {    int code;
  90.         if ( (code = restore_check_stack(osbot, op, asave)) < 0 ||
  91.              (code = restore_check_stack(esbot, esp + 1, asave)) < 0 ||
  92.              (code = restore_check_stack(dsbot, dsp + 1, asave)) < 0
  93.            )
  94.             return code;
  95.        }
  96.     if ( alloc_restore_state_check(asave) < 0 )
  97.         return_error(e_invalidrestore);
  98.     /* Invalidate any other copies of this save object on the stacks, */
  99.     /* and reset l_new in all stack entries if the new restore level */
  100.     /* is zero. */
  101.     restore_fix_stack(osbot, op, asave);
  102.     restore_fix_stack(esbot, esp + 1, asave);
  103.     restore_fix_stack(dsbot, dsp + 1, asave);
  104.     /* Restore the graphics state. */
  105.     gs_grestoreall(igs);
  106.     prev = gs_state_swap_saved(igs, (gs_state *)0);
  107.     gs_state_swap_saved(prev, vmsave->gsave);
  108.     gs_state_swap_saved(igs, prev);
  109.     gs_grestore(igs);
  110.     gs_grestore(igs);
  111.     /* Now it's safe to restore the state of memory. */
  112.     alloc_restore_state(asave);
  113.     alloc_free((char *)vmsave, 1, sizeof(vm_save), "zrestore");
  114.     pop(1);
  115.     init_identity_matrix();        /* update l_new attribute */
  116.     return 0;
  117. }
  118. /* Check a stack to make sure all its elements are older than a save. */
  119. private int
  120. restore_check_stack(const ref *bot, const ref *top, const alloc_save *asave)
  121. {    const ref *stkp;
  122.     for ( stkp = bot; stkp < top; stkp++ )
  123.        {    char *ptr;
  124.         switch ( r_type(stkp) )
  125.            {
  126.         case t_array: ptr = (char *)stkp->value.refs; break;
  127.         case t_condition: ptr = (char *)stkp->value.pcond; break;
  128.         case t_dictionary: ptr = (char *)stkp->value.pdict; break;
  129.         case t_fontID: ptr = (char *)stkp->value.pfont; break;
  130.         case t_gstate: ptr = (char *)stkp->value.pgstate; break;
  131.         /* case t_file: ****** WHAT? ****** */
  132.         case t_lock: ptr = (char *)stkp->value.plock; break;
  133.         case t_name:
  134.             /* Names are special because of how they are allocated. */
  135.             if ( alloc_name_is_since_save(stkp, asave) )
  136.                 return_error(e_invalidrestore);
  137.             continue;
  138.         case t_save: ptr = (char *)stkp->value.psave; break;
  139.         case t_string: ptr = (char *)stkp->value.bytes; break;
  140.         case t_mixedarray: case t_shortarray:
  141.             ptr = (char *)stkp->value.packed; break;
  142.         case t_device: ptr = (char *)stkp->value.pdevice; break;
  143.         default: continue;
  144.            }
  145.         if ( alloc_is_since_save(ptr, asave) )
  146.             return_error(e_invalidrestore);
  147.        }
  148.  
  149.     return 0;            /* OK */
  150. }
  151. /* Fix up the contents of a stack by invalidating */
  152. /* any save objects newer than the save being restored, */
  153. /* and, if the new save level is zero, clearing the l_new */
  154. /* bit in all the entries (since we can't tolerate values with */
  155. /* l_new set if the save level is zero). */
  156. private void
  157. restore_fix_stack(ref *bot, ref *top, const alloc_save *asave)
  158. {    ref *stkp;
  159.     for ( stkp = bot; stkp < top; stkp++ )
  160.        {    if ( r_type(stkp) == t_save &&
  161.              stkp->value.psave != 0 &&
  162.              (stkp->value.psave->asave == asave ||
  163.               alloc_is_since_save((const char *)stkp->value.psave, asave))
  164.            )
  165.             stkp->value.psave = 0;
  166.         r_clear_attrs(stkp, l_new);    /* always do it, no harm */
  167.        }
  168. }
  169.  
  170. /* - vmstatus <save_level> <vm_used> <vm_maximum> */
  171. int
  172. zvmstatus(register os_ptr op)
  173. {    long used, total;
  174.     alloc_status(&used, &total);
  175.     push(3);
  176.     make_int(op - 2, alloc_save_level());
  177.     make_int(op - 1, used);
  178.     make_int(op, total);
  179.     return 0;
  180. }
  181.  
  182. /* ------ Initialization procedure ------ */
  183.  
  184. op_def zvmem_op_defs[] = {
  185.     {"1restore", zrestore},
  186.     {"0save", zsave},
  187.     {"0vmstatus", zvmstatus},
  188.     op_def_end(0)
  189. };
  190.